package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.vo.AttrInfoVo;
import com.atguigu.gulimall.product.vo.Attrs;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private CategoryDao categoryDao;
    @Resource
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Resource
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatelogId(Map<String, Object> params, Long catelogId) {
        if(catelogId == 0) {
            String key = (String) params.get("key");
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(key)) {
                wrapper.and((obj) -> {
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );

            return new PageUtils(page);
        }else {
            String key = (String) params.get("key");
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("catelog_id",catelogId);
            if(!StringUtils.isEmpty(key)) {
                wrapper.and((obj) -> {
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }

    }

    @Override
    public AttrGroupEntity getDetailById(Long attrGroupId) {
        AttrGroupEntity entity = this.getById(attrGroupId);
        ArrayList<Long> paths = new ArrayList<>();
        ArrayList cateLogPath = this.findCateLogPath(entity.getCatelogId(), paths);
        Collections.reverse(cateLogPath);
        Long[] realPath = (Long[]) cateLogPath.toArray(new Long[cateLogPath.size()]);
        entity.setCatelogPath(realPath);

        return entity;
    }


    @Override
    public PageUtils getDetailInfoBycatelogId(Map<String, Object> params,Long catelogId) {
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)) {

            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
            QueryWrapper<AttrGroupEntity> entityQueryWrapper = wrapper.eq("catelog_id", catelogId).and((obj -> {
                obj.like("attr_group_name", key).or().eq("attr_group_id", key).or().like("descript", key);
            }));
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    entityQueryWrapper);
            return new PageUtils(page);
        }else {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>()
            );

            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrInfoVo> getSpuInfoByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrInfoVo> collect = attrGroupEntities.stream().map(obj -> {
            AttrInfoVo attrInfoVo = new AttrInfoVo();
            BeanUtils.copyProperties(obj, attrInfoVo);
            List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", obj.getAttrGroupId()));
            List<Attrs> attrsList = attrAttrgroupRelationEntities.stream().map(obj1 -> {
                Attrs attrs = new Attrs();
                AttrEntity attrEntity = attrDao.selectOne(new QueryWrapper<AttrEntity>().eq("attr_id", obj1.getAttrId()));
                BeanUtils.copyProperties(attrEntity, attrs);
                return attrs;
            }).collect(Collectors.toList());
            attrInfoVo.setAttrs(attrsList);

            return attrInfoVo;
        }).collect(Collectors.toList());

        return collect;
    }

    public ArrayList findCateLogPath(Long catelogId, ArrayList<Long> paths) {
        if(catelogId != 0){
            paths.add(catelogId);
            Long parentCid = categoryDao.selectById(catelogId).getParentCid();
            this.findCateLogPath(parentCid,paths);
        }
        return paths;
    }
}
